using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
//using System.Data.SqlTypes;
using System.Diagnostics;


namespace FilmekAdatbazis
{
	/// <summary>
	/// Summary description for Aru.
	/// </summary>
	public class Aru : DAL
	{
		//		private System.Data.SqlClient.SqlCommand myUdateComm;
		private System.Data.SqlClient.SqlDataAdapter daGrid;
		private System.Data.SqlClient.SqlDataAdapter daAruk; 
		private System.Data.DataSet dsArukGrid;
		private System.Data.DataSet dsAruk;
		private System.Data.DataSet dsEgyAruSor;
		private System.Data.SqlClient.SqlDataAdapter daEgyAruSor;
		private string ErrMess;
		private string sQuery;
		public Aru(ref bool Err)
		{
			if (!base.IsConnectCreated())
			{
				Err = !base.MakeConnection();
			}
			daAruk = new SqlDataAdapter();
			daAruk.TableMappings.Add("Table", "Filmek");
			daEgyAruSor = new SqlDataAdapter();
			daEgyAruSor.TableMappings.Add("Table", "EgyAru");
			//			dsArukGrid = new DataSet("ArukGrid");
			daGrid = new SqlDataAdapter();
			daGrid.TableMappings.Add("Table", "ArukGrid");
		}
	
		public System.Data.DataSet AruGridTolt(string sBetuk, ref string ErrM)
		{
			string sSQL = "SELECT FilmID, Cim, " +
				"Stilusok.Stilus " + 
				" FROM Filmek,Stilusok where Filmek.StilusID =" +
				" Stilusok.StilusID and Cim LIKE '" + sBetuk + "%'";
			dsArukGrid = base.myFillDataSetFromString(sSQL,"ArukGrid",ref daGrid,ref ErrM);
			return dsArukGrid;
			
		}
        public System.Data.SqlClient.SqlDataReader ReszletekLbTolt(string skod, ref string ErrM)
        {
            String query = "SELECT FilmID,Reszletek FROM Filmek WHERE FilmID=" + skod;
            return base.myExecuteReader(query, ref ErrM);
        }

        public System.Data.SqlClient.SqlDataReader getFelhasznalok(ref String ErrM)
        {
            myDataReader = base.myExecuteReader("SELECT FelhasznaloNev FROM Felhasznalok", ref ErrM);
            return myDataReader;
        }

        public static void ListBoxEpito(System.Windows.Forms.ListBox lb, System.Data.SqlClient.SqlDataReader DtReadcombo, ref ArrayList aKOD)
        {
            lb.Items.Clear();
            while (DtReadcombo.Read())
            {
                lb.Items.Add(DtReadcombo[1].ToString());
                aKOD.Add(DtReadcombo[0].ToString());
            }
            DtReadcombo.Close();

        }

        public static void ListBoxEpito1(System.Windows.Forms.ListBox lb, System.Data.SqlClient.SqlDataReader DtReadcombo)
        {
            lb.Items.Clear();
            while (DtReadcombo.Read())
            {
                lb.Items.Add(DtReadcombo[0].ToString());
            }
            DtReadcombo.Close();

        }

		public System.Data.DataSet ArukTolt(ref string ErrM)
		{
			string sSQL = "SELECT * FROM Filmek";
			dsAruk = base.myFillDataSetFromString(sSQL,"Aruk",ref daAruk,ref ErrM);
			return dsAruk;
			
		}
		public System.Data.DataSet AruEgySorTolt(string sArukod, ref string ErrM)
		{
			String sSQL = "SELECT * FROM Filmek WHERE FilmID="+sArukod;

			dsEgyAruSor = base.myFillDataSetFromString(sSQL,"EgyAru", ref daEgyAruSor,ref ErrM);
			return dsEgyAruSor;							
		}

       

		public void EgySorModosit(System.Data.DataRow drEgySor,	ref string Err)
		{
			base.OpenConnection();
			daEgyAruSor.UpdateCommand = new SqlCommand("UPDATE Filmek SET Cim = @Cim " +
				", StilusID = @StilusID , Reszletek = @Reszletek "+
				"WHERE FilmID = @FilmID" ,base.theConnection());
			daEgyAruSor.UpdateCommand.Parameters.Add("@Cim", SqlDbType.VarChar, 40, "Cim");
			daEgyAruSor.UpdateCommand.Parameters.Add("@Reszletek", SqlDbType.VarChar, 40, "Reszletek");
			daEgyAruSor.UpdateCommand.Parameters.Add("@StilusID", SqlDbType.Int, 8, "StilusID");
			SqlParameter workParm = daEgyAruSor.UpdateCommand.Parameters.Add("@FilmID", SqlDbType.Int);
			workParm.SourceColumn = "FilmID";
			//workParm.SourceVersion = DataRowVersion.Original;
			try
			{
				daEgyAruSor.Update(dsEgyAruSor,"EgyAru");
				Err = "OK";
			}
			catch( DBConcurrencyException ex )
			{
				Err = ex.ToString();
				dsEgyAruSor.RejectChanges();
			}
							
			base.CloseConnection();
			
		}
		public System.Data.DataSet RefreshAruGrid(ref string Err)
		{
			base.OpenConnection();
			try
			{
				dsArukGrid.Clear();
				daGrid.Fill(dsArukGrid);				
				Err = "OK";
				return dsArukGrid;
			}
			catch( Exception ex )
			{
				Err = ex.ToString();
				return null;
			}
			finally
			{
				base.CloseConnection();
			}
		}
		public int MaxAruKod(ref string Err)
		{
			sQuery = "SELECT MAX(FilmID) FROM Filmek";
			int iKod = base.myExecuteScalar(sQuery, ref Err);
			return iKod;
		}

				public void InsertParancs()
					//int iKod, string sNev, int iCsopKod, Double MennyRakt,
					//ref string Err)
				{
					
					daAruk.InsertCommand = new SqlCommand("INSERT INTO Aruk VALUES (?, ?, ?, ?) ");
                    daAruk.InsertCommand.Parameters.Add("@FilmID", SqlDbType.Int, 8, "FilmID");
					daAruk.InsertCommand.Parameters.Add("@Cim", SqlDbType.VarChar, 40,"Cim");
					daAruk.InsertCommand.Parameters.Add("@StilusID", SqlDbType.Int, 8, "StilusID");
					daAruk.InsertCommand.Parameters.Add("@Reszletek", SqlDbType.VarChar, 40, "Reszletek");
					daAruk.InsertCommand.Connection = base.theConnection();	
//					base.OpenConnection();
//					try
//					{
//						daAruk.Fill(dsAruk);
//						DataRow drAruSor = dsAruk.Tables["Aruk"].NewRow();
//						drAruSor.ItemArray = new object[] {iKod, sNev,  iCsopKod, MennyRakt};
//						dsAruk.Tables["Aruk"].Rows.Add(drAruSor);
//						daAruk.Update( dsAruk, "Aruk");
//						Err = "OK";
//					}
//					catch( Exception ex )
//					{
//						Err = ex.ToString();
//						dsEgyAruSor.RejectChanges();
//					}
//					base.CloseConnection();
				}
				
		public void UjSorIr(int iKod, string sNev, int iCsopKod, string Reszlet,
			ref string Err)
		{
            int motorid = iKod;
            String gyarto = sNev;
            int b = iCsopKod;
            String c = Reszlet;
            try
            {
                mySQLServerConn.Open();
                SqlCommand cmd = new SqlCommand("Filmekk", mySQLServerConn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@FilmID", motorid);
                cmd.Parameters.Add("@Cim", gyarto);
                cmd.Parameters.Add("@StilusId", b);
                cmd.Parameters.Add("@Reszletek", c);
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
            }
            finally
            {
                mySQLServerConn.Close();
            }

            //sQuery = "INSERT INTO Filmek VALUES (" +
            //    Convert.ToString(iKod) + "," + "'" + sNev + "'" + "," +Convert.ToString(iCsopKod) +
            //    ",'" + Reszlet + "' ) ";
            //base.myInsertCommand(sQuery,daAruk,ref Err);
			
		}
		public void TorolEgySort(int iKod, ref string Err)
		{
			sQuery = "DELETE FROM Filmek WHERE FilmID = " +Convert.ToString(iKod);
			MyDeleteComm(sQuery,daEgyAruSor,ref Err);
		}

        public void TorolEgySort1(int iKod, ref string Err)
        {
            sQuery = "DELETE FROM DVDk WHERE FilmID = " + Convert.ToString(iKod);
            MyDeleteComm(sQuery, daEgyAruSor, ref Err);
        }
		public void WriteDataSetInDB(DataSet dsAruk, ref string Err)
		{
			InsertParancs();
			DataTable NewChildRecords = dsAruk.Tables["Filmek"].GetChanges(DataRowState.Added);
			DataTable DeletedChildRecords = dsAruk.Tables["Filmek"].GetChanges(DataRowState.Deleted);
			//workTable.PrimaryKey = new DataColumn[] {workTable.Columns["CustID"]};

			dsAruk.Tables["Aruk"].PrimaryKey= new DataColumn [] {dsAruk.Tables["Filmek"].Columns["FilmID"]};
			try
			{
				if (DeletedChildRecords != null)
				{
					daAruk.Update(DeletedChildRecords);
					DeletedChildRecords.Dispose();
				}
				if (NewChildRecords != null)
				{
			//		daAruk.Update(NewChildRecords);
					daAruk.Update(dsAruk,"Aruk");
					NewChildRecords.Dispose();
				}
			}
			catch(Exception ex)
			{
				Err = ex.ToString();
				dsAruk.RejectChanges();
				
			}
		}
	}
}
